package com.guochaojava.api;

import com.guochaojava.model.Member;
import com.guochaojava.param.MemberParam;
import com.guochaojava.service.MemberService;
import com.guochaojava.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 会员相关接口
 *
 * @author guochao
 * @since 1.0.0
 */
@Api(tags = "会员用户接口")
@Validated
@RestController
@RequestMapping("/member")
public class MemberApi {

    @Autowired
    private MemberService memberService;

    @ApiOperation(value = "保存信息")
    @PostMapping("/save")
    public Object save(@Valid MemberParam param, HttpServletRequest request) {
        try {
            memberService.insertSelective(param, request);
            return Result.buildOK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.buildError();
        }
    }

    @ApiOperation(value = "显示我的二维码")
    @PostMapping("/show")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openId", value = "用户openid", dataType = "string", paramType = "query")
    })
    public Object show(@NotNull String openId) {
        try {
            Member member = memberService.selectByOpenId(openId);
            return Result.buildOK(member.getQrCode(),"获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.buildError();
        }
    }

    @ApiOperation(value = "查询选手信息")
    @PostMapping("/info")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "选手主键id", dataType = "int", paramType = "query")
    })
    public Object info(@NotNull Integer id) {
        try {
            Member member = memberService.selectByPrimaryKey(id);
            return Result.buildOK(member);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.buildError();
        }
    }
}